﻿using System;
using System.Collections.Generic;
using System.Text;
using Avalonia;
using Avalonia.Controls;
using Avalonia.ReactiveUI;
using ReactiveUI;

namespace AvaloniaApplication1.Models
{
   public class TestReactiveUserControl<TViewModel> :UserControl, IViewFor<TViewModel> where TViewModel : class
   {
       public static readonly StyledProperty<TViewModel> ViewModelProperty = AvaloniaProperty
           .Register<ReactiveUserControl<TViewModel>, TViewModel>(nameof(ViewModel));

        

        public TestReactiveUserControl()
        {
            DataContextChanged += (sender, args) => ViewModel = DataContext as TViewModel;
        }

        public TViewModel GetViewModel()
        {
            return ViewModel;


        }

        /// <summary>
        /// The ViewModel.
        /// </summary>
        public TViewModel ViewModel
        {
            get => GetValue(ViewModelProperty);
            set => SetValue(ViewModelProperty, value);
        }

        object IViewFor.ViewModel
        {
            get => ViewModel;
            set => ViewModel = (TViewModel)value;
        }
    }
}
